<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'north',border:false">
		<form class="appForm">
			<div class="datagrid-toolbar">
				<table class="appTable">
					<tr>
						<td>
							<input name="name" data-options="prompt:'菜单名称'" class="easyui-textbox" type="text" style="width:100px;"/>
							<button type="submit" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</button>
							<button type="button" onclick="$(this).parents('form').eq(0).form('reset');" class="easyui-linkbutton" data-options="iconCls:'icon icon-reset'">清空</button>
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div data-options="region:'center',border:false">
		<table class="appGrid" isTree="true"></table>
	</div>
</div>
<script type="text/javascript">
	var grids = $("table.appGrid");
	grids.eq(grids.size()-1).treegrid({
		url:_ctx+'/admin/menu/list',
		columns:[[
			{field:'id',checkbox:true},
			{field:'name',title:'名称',width:100},
			{field:'icon',title:'图标',width:60,
				formatter: function(val,row,index){
					return '<span class="icon icon-width '+val+'"></span>';
				}
			},
			{field:'orders',title:'排序',width:60},
			{field:'type',title:'分类',width:80,
				formatter: function(val){
					return $.appDicFormat('menu_type',val);
				}
			},
			{field:'url',title:'链接',width:150}
		]],
		appToolbar: [{
			iconCls: 'icon icon-auth',
			"key":'menu_access',
			text:'菜单授权',
			handler: function(){
				var grid = getDatagrid(this);
				var rows = grid.treegrid("getSelections");
				if(rows.length==0){
					$.appAlert("请选择要授权的菜单！");
					return;
				}
				var ids = [];
				$.each(rows,function(i,r){
					ids.push(r.id);
				});
				$("<div/>").dialog({
					title: '菜单授权',
					href: _tpl+'/admin/menu_auth.tpl.php',
					width:300,
					height:440,
					data:{'menuIds':ids.join(',')},
					buttons:[
						{
							text : '保存',
							iconCls : 'icon-save',
							handler : function() {
								var form = $(this).parents("div.window").eq(0).find("form").eq(0);
								var rows = $("div#allRoleList2").datalist("getSelections");
								var ids = [];
								for(var i=0;i<rows.length;i++){
									ids.push(rows[i].id);
								}
								form.find("input[name='roleIds']").val(ids.join(','));
								form.submit();
							}
						}
					]
				});
			}
		},{
			iconCls: 'icon icon-layout-left',
			text:'生成菜单',
			"key":'menu_create',
			handler: function(){
				loadJs(_ctx+"/config/menu.js?"+new Date().getTime(),function(){
					$.each(_moduals,function(i,r){
						r['menus'] = _menus[r.key];
					});
					var menus = [];
					function each($_emenus,$_pid){
						$.each($_emenus,function(i,r){
							r.orders = ++i;
							if($_pid){
								r.parent_key = $_pid;
							}else{
								r['type'] = 1;
							}
							if(!r['type']){
								if(!r['url']){
									r['type'] = 2;
								}else if(!r['isfun']){
									r['type'] = 3;
								}else{
									r['type'] = 4;
									r['is_show'] = 0;
								}
							}
							if(r.menus) {
								r['is_leaf'] = 0;
								menus.push(json2Str(r));
								each(r.menus,r.key);
								delete(r.menus);
							}else{
								r['is_leaf'] = 1;
								menus.push(json2Str(r));
							}
						});
					}
					each(_moduals,null);
					var grid = getDatagrid(this);
					$.appAjax({
						url:_ctx+'/admin/menu/create',
						data:{menus:menus},
						grid:grid.get(0)
					});
				});
			}
		}]
	});
</script>